package patrol.mobile.com.mobilepatrol.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.bean.User;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.persenter.UserPer;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.Digests;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.view.UserView;
import patrol.mobile.com.mobilepatrol.widget.dialog.SerEditDialog;

/**
 * Created by apple on 17/9/27.
 */

public class LoginActivity extends BaseActivity implements UserView {
    @BindView(R.id.et_phone)
    public EditText et_phone;

    @BindView(R.id.et_pwd)
    public EditText et_pwd;

    @BindView(R.id.bt_login)
    public Button bt_login;

    @BindView(R.id.tv_ser)
    public TextView tv_ser;

    @BindView(R.id.tv_set)
    public TextView tv_set;

    private String user;
    private String pwd;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    public void setListener() {
        tv_ser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final SerEditDialog dialog = new SerEditDialog(LoginActivity.this, R.style.Dialog);
                dialog.show();
                dialog.setmListener(new SerEditDialog.SureListener() {
                    @Override
                    public void onSure(String ser) {
                        SPUtils.put(Constants.current_ser, ser);
                        dialog.dismiss();
                        showToast("初始化服务器成功");
                    }
                });
            }
        });

        tv_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SPUtils.get(Constants.current_ser,"").equals("")){
                    showToast("请先设置服务器");
                }else{
//                    startActivity(InitActivity.class);
//                    CommonUtil.openNewActivityAnim(LoginActivity.this, false);
                    showLoading();
                    UserPer userPer = new UserPer(LoginActivity.this);
                    userPer.getUsers();
                }

            }
        });
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//

                user = et_phone.getText().toString().trim();
                pwd = et_pwd.getText().toString().trim();

//                boolean isExit = UserManager.isExit(user);
//                User u = UserManager.isExit(user,pwd);
                if (user.isEmpty()) {
                    showToast("请输入用户名");
                    return;
                }
                if (pwd.isEmpty()) {
                    showToast("请输入密码");
                    return;
                }
                pwd = Digests.md5(pwd);
                showLoading();
                boolean isExit = UserManager.isExit2(user);
                if (isExit) {
                    User u = UserManager.isExit2(user, pwd);
                    if (u == null) {
                        showToast("账号密码错误");
                        hideLoading();
                    } else {
                        SPUtils.put(LoginActivity.this, Constants.current_id, u.ryid);
//                        SPUtils.put(LoginActivity.this, Constants.current_username, u.username);
//                        SPUtils.put(LoginActivity.this, Constants.current_userpwd, u.pw);
//                        SPUtils.put(LoginActivity.this, Constants.current_ryname, u.ryname);
                        hideLoading();
                        startActivity(MainActivity.class);
                        CommonUtil.openNewActivityAnim(LoginActivity.this, true);
//                        Map<String, String> map = new HashMap<>();
//                        map.put("roadcode", "G30");
////                      map.put("spos","K2851");
////                      map.put("epos","K2923");
//                        QHMesPer qhMesPer = new QHMesPer(LoginActivity.this, map);
//                        qhMesPer.getQHMes();
                    }
                } else {
                    showToast("账号不存在");
                    hideLoading();
                }
            }
        });
    }

    @Override
    public void initTitle() {
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("账号登录");
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onUserError(String s) {
        hideLoading();
        showToast(s);
    }

    @Override
    public void onUserSuccess(String s) {
        hideLoading();
        showToast(s);
    }
}
